﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;


class BagServerMgr
{
    //道具唯一ID集合
    private static Hashtable ItemIdTable;
    //道具在背包格子集合
    private static Hashtable ItemGridIdTable;
    //道具模型数据集合
    private static Hashtable itemModelTable;

    public BagServerMgr()
    {
        Init();
    }

    private void Init()
    {
        itemModelTable = new Hashtable();
        ItemInfo[] infos = Mgr.configMgr.ReaderConfigFile<ItemInfo[]>("ItemJson");
        foreach(ItemInfo item in infos)
        {
            itemModelTable.Add(item.ItemModelId, item);
        }
    }

    //上线向前端发送背包数据
    public void ReqBagTotalItemList( List<ItemInfo> itemList)
    {
        if (itemList == null)
            itemList = new List<ItemInfo>();
        if(ItemIdTable == null)
            ItemIdTable = new Hashtable();
        if (ItemGridIdTable == null)
            ItemGridIdTable = new Hashtable();
        foreach (ItemInfo info in itemList)
        {
            ItemIdTable.Add(info.ItemId, info);
            ItemGridIdTable.Add(info.GridId, info);
        }

        Mgr.bagMgr.ResBagTotalItemList(itemList);
    }

    //发送道具增加结果
    public void ReqBagAddItem(ItemInfo item)
    {
        ItemIdTable.Add(item.ItemId, item);
        ItemGridIdTable.Add(item.GridId, item);
        Mgr.bagMgr.ResBagAddItem(item);
    }

    //发送道具删除结果
    public void ReqDelItem(ItemInfo item)
    {
        ItemIdTable.Remove(item.ItemId);
        ItemGridIdTable.Add(item.GridId, item);
        Mgr.bagMgr.ResBagDelItem(item);
    }

    //发送道具改变结果
    public void ReqChangeItem(ItemInfo item)
    {
        ItemIdTable[item.ItemId] = item;
        ItemGridIdTable[item.GridId] = item;
        Mgr.bagMgr.ResBagChangeItem(item);
    }

    //收到道具使用请求
    public void ResUseItem(ItemInfo item)
    {
        int useNum = item.ItemNum;
        int enough = CheckItemEnough(item, useNum);

        if(enough > 0)
        {
            if(CheckItemUse(item, useNum))
            {
                //使用道具效果
                UseItem(item);

                //删除道具数量
                if (enough == 2)
                {
                    item.ItemNum -= useNum;
                    ItemIdTable[item.ItemId] = item;
                    Mgr.bagMgr.ResBagChangeItem(item);
                }
                else
                {
                    ItemIdTable.Remove(item.ItemId);
                    Mgr.bagMgr.ResBagDelItem(item);
                }
                Mgr.bagMgr.ResUseItem(item.ItemId, useNum, true);
            }
        }
        else
        {
            //数量不足，使用失败
            Debug.Log("道具" + item.ItemName + "使用失败，数量不足！（" + item.ItemNum + "/" + useNum + ")");
            Mgr.bagMgr.ResUseItem(item.ItemId, useNum, false);
        }
        
    }

    private void UseItem(ItemInfo item)
    {
        switch(item.ItemModelId)
        {
            //红药水
            case EnumItem.HPDrug:
                int curHp = Mgr.mainPlayer.info.Health;
                int maxHp = Mgr.mainPlayer.MaxHealth;
                curHp = Mathf.Min(maxHp, curHp + 20);
                Mgr.fightMgr.fightEvent.DispatchEvent(FightEvent.PlayerChangeHp, new int[2] { curHp, maxHp });
                break;
        }
    }

    //判断道具数量是否足够使用  0：不够  1：刚刚好   2：有多余的
    public int CheckItemEnough(ItemInfo item, int useNum)
    {
        if (ItemIdTable.ContainsKey(item.ItemId))
        {
            ItemInfo info = (ItemInfo)ItemIdTable[item.ItemId];
            {
                if (info.ItemNum > useNum)
                    return 2;
                else if (info.ItemNum == useNum)
                    return 1;
            }
        }
        return 0;
    }
    //判断是否可以使用道具
    public bool CheckItemUse(ItemInfo item, int useNum)
    {
        

        return true;
    }

    //道具位置改变
    public void ResMoveItem(ItemInfo item, int toGridId)
    {

        if(ItemGridIdTable.ContainsKey(toGridId))
        {//目标格子上有道具则交换
            ItemInfo item2 = (ItemInfo)ItemGridIdTable[toGridId];
            item2.GridId = item.GridId;
            ItemGridIdTable[item2.GridId] = item2;
            ReqMoveItem(item2, item2.GridId);

            item.GridId = toGridId;
            ItemGridIdTable[toGridId] = item;
            ReqMoveItem(item, item.GridId);

        }
        else
        {
            int oldId = item.GridId;
            item.GridId = toGridId;
            ItemGridIdTable[item.GridId] = item;
            ItemGridIdTable.Remove(oldId);
            ReqMoveItem(item, toGridId);
            ReqMoveItem(null, oldId);

        }
    }
    //发送道具位置改变
    //如果item为null，则删除oldGridId位置的道具
    public void ReqMoveItem(ItemInfo item, int GridId)
    {
        Mgr.bagMgr.ResMoveItem(item, GridId);
    }

    //增加一个道具
    public void AddItem(int itemId)
    {
        ItemInfo item = itemModelTable[itemId] as ItemInfo;
        item = item.Clone();

        //随机生成唯一ID
        int id;
        do
        {
            id = Random.Range(100000, 999999);
        }
        while (ItemIdTable.ContainsKey(id));
        item.ItemId = id;

        int bagSize = Mgr.bagMgr.BagSize;
        for(int i = 1;i<=bagSize;i++)
        {
            if(!ItemGridIdTable.ContainsKey(i))
            {
                item.GridId = i;
                break;
            }
        }

        ReqBagAddItem(item);

    }

    public ItemInfo GetItemModelInfo(int itemModelId)
    {
        return itemModelTable[itemModelId] as ItemInfo;
    }
    

    //===================================================
    private static BagServerMgr _instance;
    public static BagServerMgr Instance
    {
        private set { }
        get
        {
            if (_instance == null)
                _instance = new BagServerMgr();
            return _instance;
        }
    }
}
